package com.tiancheng.trade.merchant.controller.api;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.web.model.request.MerchantChannelRequestDTO;
import com.tiancheng.trade.commom.web.model.request.QueryOrderMerchantReqVO;
import com.tiancheng.trade.commom.web.model.response.MerchantChannelResponseDTO;
import com.tiancheng.trade.commom.web.model.response.QueryOrderMerchantResVO;
import com.tiancheng.trade.merchant.dto.responsedto.MerchantWithChannelDTO;
import com.tiancheng.trade.merchant.exception.BusinessException;
import com.tiancheng.trade.merchant.model.Merchant;
import com.tiancheng.trade.merchant.model.MerchantPaymentChannel;
import com.tiancheng.trade.merchant.service.IMerchantPayRegService;
import com.tiancheng.trade.merchant.service.IMerchantPaymentChannelService;
import com.tiancheng.trade.merchant.service.IMerchantService;
import com.tiancheng.trade.merchant.vo.merchant.GetMerchantPaymentVO;
import com.tiancheng.trade.merchant.vo.merchant.GetMerchantVO;
import com.tiancheng.trade.merchant.vo.merchant.GetPageMerchantVO;
import com.tiancheng.trade.merchant.vo.merchant.MerchantPaymentChannelVO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/merchant")
public class MerchantController {
    @Resource
    private IMerchantService merchantService;
    @Resource
    private IMerchantPaymentChannelService channelService;
    @Resource
    private IMerchantPayRegService merchantPayRegService;

    /**
     * 按交易中台商户编码和支付渠道编码查询渠道信息 - 支付中心使用
     *
     * @param merchantCode       交易中台商户编码
     * @param paymentChannelCode 支付渠道编码
     * @return {@link Result}<{@link MerchantPaymentChannel}>
     */
    @GetMapping("/findSubAppid")
    public Result<MerchantPaymentChannel> findSubAppid(@RequestParam(name = "merchant_code") String merchantCode,
                                                       @RequestParam(name = "payment_channel_code") String paymentChannelCode)
            throws BusinessException {

        GetMerchantPaymentVO paymentVO = new GetMerchantPaymentVO();
        paymentVO.setMerchantCode(merchantCode);
        paymentVO.setPaymentChannelCode(paymentChannelCode);
        return Result.success(channelService.findSubAppid(paymentVO));
    }

    /**
     * 按商户编码和支付渠道编码批量查询渠道信息列表 - 支付中心使用
     * todo 创始人采用循环查询得到结果，空闲时建议优化查询方式
     *
     * @param list {@link List}<{@link GetMerchantPaymentVO}>
     * @return {@link Result}<{@link List}<{@link MerchantPaymentChannel}>>
     */
    @PostMapping("/findSubAppidList")
    public Result<List<MerchantPaymentChannel>> findSubAppidList(@RequestBody List<GetMerchantPaymentVO> list)
            throws BusinessException {

        Assert.isTrue(!(list == null || list.isEmpty()), "参数错误~");
        List<MerchantPaymentChannel> resultList = new ArrayList<>();
        for (GetMerchantPaymentVO merchantVO : list) {
            GetMerchantPaymentVO paymentVO = new GetMerchantPaymentVO();
            paymentVO.setMerchantCode(merchantVO.getMerchantCode());
            paymentVO.setPaymentChannelCode(merchantVO.getPaymentChannelCode());
            MerchantPaymentChannel pay = channelService.findSubAppid(paymentVO);
            if (pay != null) {
                resultList.add(pay);
            }
        }
        return Result.success(resultList);
    }

    /**
     * 根据交易中台商户编码和支付渠道编码批量查询商户信息列表
     *
     * @param list {@link List}<{@link GetMerchantPaymentVO}>
     * @return {@link Result}<{@link List}<{@link MerchantWithChannelDTO}>>
     */
    @PostMapping("/listByMerchantCodeAndChannel")
    public Result<List<MerchantChannelResponseDTO>> listByMerchantCodeAndChannel(@RequestBody List<MerchantChannelRequestDTO> list)
            throws BusinessException {

        Assert.isTrue(!(list == null || list.isEmpty()), "参数错误~");
        return Result.success(merchantPayRegService.listSubMerchant(list));
    }


    /**
     * 查询机构的子商户，分账时用
     *
     * @param orgPaymentChannelId 机构支付渠道id
     * @return {@link Result}<{@link List}<{@link MerchantPaymentChannel}>>
     */
    @PostMapping("/queryOrgSubMerchant")
    public Result<List<MerchantChannelResponseDTO>> queryOrgSubMerchant(@RequestBody() List<Long> orgPaymentChannelId)
            throws BusinessException {

        Assert.isTrue(!(orgPaymentChannelId == null || orgPaymentChannelId.isEmpty()), "参数错误~");
        List<MerchantChannelResponseDTO> merchantChannelResponseDTOS = merchantPayRegService.queryOrgSubMerchant(orgPaymentChannelId);
        return Result.success(merchantChannelResponseDTOS);
    }

    /**
     * 查询一个商户信息
     *
     * @param merchantVO {@link GetPageMerchantVO}
     * @return {@link Result}<{@link Merchant}>
     */
    @GetMapping("/findMerchant")
    public Result<Merchant> findMerchant(GetPageMerchantVO merchantVO) throws BusinessException {
        Merchant result = merchantService.findMerchantOrg(merchantVO);
        return Result.success(result);
    }

    /**
     * 按组织编号查询商户信息列表
     *
     * @param merchantVO {@link GetMerchantVO}
     * @return {@link Result}<{@link List}<{@link Merchant}>>
     */
    @GetMapping("/findMerchantByOrgCode")
    public Result<List<Merchant>> findMerchantByOrgCode(GetMerchantVO merchantVO) throws BusinessException {
        return Result.success(merchantService.findMerchantByOrgCode(merchantVO));
    }

    /**
     * 根据业务侧商户编号，查询商户信息列表
     *
     * @param channelVO {@link MerchantPaymentChannelVO}
     * @return {@link Result}<{@link List}<{@link MerchantPaymentChannelVO}>>
     */
    @GetMapping("/findMerchantByBusiness")
    public Result<List<MerchantPaymentChannelVO>> findMerchantByBusiness(MerchantPaymentChannelVO channelVO)
            throws BusinessException {

        return Result.success(merchantService.findMerchantByBusiness(channelVO));
    }

    /**
     * 按交易中台商户编码批量查询商户信息列表
     *
     * @param merchantCodes {@link List}<{@link String}>
     * @return {@link Result}<{@link List}<{@link Merchant}>>
     */
    @GetMapping("/getMerchantByMerCode")
    public Result<List<Merchant>> getMerchantByMerCode(@RequestParam("list") List<String> merchantCodes)
            throws BusinessException {

        return Result.success(merchantService.getMerchantByMerCode(merchantCodes));
    }

    /**
     * 根据商户名称模糊查询商户信息列表
     *
     * @param merchantName {@link String}
     * @return {@link Result}<{@link List}<{@link GetMerchantVO}>>
     */
    @GetMapping("/getMerchantsByName")
    public Result<List<GetMerchantVO>> getMerchantsByName(@RequestParam(value = "merchant_name") String merchantName) {
        return Result.success(merchantService.getMerchantsByName(merchantName));
    }

    /**
     * 根据交易中台 商户编号 商户信息和支付渠道信息
     *
     * @param list {@link List}<{@link String}> 交易中台商品编号列表
     * @return {@link Result}<{@link List}<{@link MerchantPaymentChannelVO}>> 商品和支付渠道信息列表
     */
    @GetMapping("/findPaymentChannelByMerchantCodes")
    public Result<List<MerchantPaymentChannelVO>> findPaymentChannelByMerchantCodes(@RequestParam("list") List<String> list)
            throws BusinessException {

        return Result.success(merchantService.findPaymentChannelByMerchantCodes(list));
    }

    /**
     * 查询下单商户信息
     *
     * @param queryOrderMerchantReqVO
     * @return
     */
    @PostMapping("/queryOrderMerchant")
    public Result<List<QueryOrderMerchantResVO>> queryOrderMerchant(@RequestBody @Validated QueryOrderMerchantReqVO queryOrderMerchantReqVO) {
        final List<QueryOrderMerchantResVO> queryOrderMerchantResVOS = merchantService.queryOrderMerchant(queryOrderMerchantReqVO);
        return Result.success(queryOrderMerchantResVOS);
    }
}
